package com.hb.trans.mvp.ui.activity


import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.amap.api.maps2d.AMap
import com.amap.api.maps2d.CameraUpdateFactory
import com.amap.api.maps2d.model.LatLng
import com.amap.api.maps2d.model.LatLngBounds
import com.amap.api.maps2d.model.PolygonOptions
import com.basiclib.base.BaseActivity
import com.hb.trans.R
import com.hb.trans.bean.TitleItem
import com.hb.trans.global.ACTIVITY_MAP
import com.hb.trans.mvp.presenter.NewDetailPresenter
import com.hb.trans.mvp.ui.adapter.MapAdapter
import com.jakewharton.rxbinding2.view.clicks
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_map.*
import java.math.BigDecimal
import java.util.*
import javax.inject.Inject


@Route(path = ACTIVITY_MAP)
class MapActivity : BaseActivity() {
    private var aMap: AMap? = null

    val point1 = LatLng(39.221245659723, 115.770625542535)
    val point2 = LatLng(39.22159233941, 115.77149468316)
    val point3 = LatLng(39.226186252171, 115.77727593316)
    val point4 = LatLng(39.216641167535, 115.78869249132)
    val point5 = LatLng(39.2135077908, 115.781258138021)
    val point1_1 = MyLocation(39.221245659723, 115.770625542535)
    val point1_2 = MyLocation(39.22159233941, 115.77149468316)
    val point1_3 = MyLocation(39.226186252171, 115.77727593316)
    val point1_4 = MyLocation(39.216641167535, 115.78869249132)
    val point1_5 = MyLocation(39.2135077908, 115.781258138021)

    private val titles1: List<String> by lazy { resources.getStringArray(R.array.company_detail_title_1).toList() }
    private val titleList1 = ArrayList<TitleItem>()

    @Inject
    lateinit var mAdapter: MapAdapter

    @Inject
    override fun getContentViewResId(): Int = R.layout.activity_map

    override fun onActivityInject() {
        AndroidInjection.inject(this)
    }

    override fun attachView(savedInstanceState: Bundle?) {

        ARouter.getInstance().inject(this)
        mapView.onCreate(savedInstanceState)

    }

    override fun getPresenter(): NewDetailPresenter? = null

    override fun initView() {
        init()

        for (index in 0 until titles1.size){
            var title = TitleItem(titles1[index],"str")
            titleList1.add(title)
        }


        mAdapter.setNewData(titleList1)
        mapMenu.apply {
            adapter = mAdapter
        }
    }


    override fun initData() {
    }

    @SuppressLint("CheckResult")
    override fun setListener() {
        mapTitle.clicks().subscribe{
            if(mapMenu.visibility==View.VISIBLE){
                mapMenu.visibility = View.GONE
            }else{
                mapMenu.visibility = View.VISIBLE
            }
        }
        mapBack.clicks().subscribe{
            onBackPressed()
        }
    }

    /**
     * 初始化AMap对象
     */
    private fun init() {
        if (aMap == null) {
            aMap = mapView.map
            setUpMap()
        }
    }

    private fun setUpMap() {
//        aMap!!.moveCamera(CameraUpdateFactory.newLatLngZoom(BEIJING, 5f))// 设置指定的可视区域地图
        // 绘制一个长方形
        aMap!!.addPolygon(
            PolygonOptions()
                .addAll(createRectangle())
                .fillColor(Color.TRANSPARENT).strokeColor(Color.RED).strokeWidth(5f)
        )


        val builder = LatLngBounds.Builder()

        for (point in createRectangle()) {
            builder.include(point)
        }
        aMap!!.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 20))
        var abcd = getArea(createRectangle1())
        var edrf = abcd
        showShortToast(edrf.toString())

    }


    private fun createRectangle(): List<LatLng> {
        return Arrays.asList(
          point1,point2,point3,point4,point5
        )
    }
    private fun createRectangle1(): List<MyLocation> {
        return Arrays.asList(
            point1_1,point1_2,point1_3,point1_4,point1_5
        )
    }



    /**
     * 方法必须重写
     */
    override fun onResume() {
        super.onResume()
        mapView.onResume()
    }


    override fun onPause() {
        super.onPause()
        mapView.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        mapView.onSaveInstanceState(outState)
    }

    override fun onDestroy() {
        super.onDestroy()
        mapView.onDestroy()
    }


    /**
     * 计算经纬度围成的实际面积（平方公里）
     * @return
     */
    private fun getArea(ring: List<MyLocation>): Double {
        var sJ = BigDecimal(6378137.0)
        var hq = BigDecimal(0.017453292519943295)

//        var c = sJ * hq
        var c = sJ.multiply(hq)
        var d = BigDecimal(0.0)

        if (3 > ring.size) {
//            return 0
        }

        for (i in 0 until ring.size - 1) {
            var h = ring[i]
            var k = ring[i + 1]

//            var u = h.longitude * c * Math.cos(h.latitude * hq)
            var u1 = BigDecimal(h.longitude).multiply(c)
            var u2 =  Math.cos(BigDecimal(h.latitude).multiply(hq).toDouble())
            var u  = u1.multiply(BigDecimal(u2))

//            var hhh = h.latitude * c
            var hhh = BigDecimal(h.latitude).multiply(c)
            var hhh1 = BigDecimal(39.221245659723).multiply(c)

//            var v = k.longitude * c * Math.cos(k.latitude * hq)
            var v1 = BigDecimal(k.longitude).multiply(c)
            var v2 = Math.cos(BigDecimal(k.latitude).multiply(hq).toDouble())
            var v = v1.multiply(BigDecimal(v2))


//            d += u * k.latitude * c - v * hhh
            var d1 = BigDecimal(k.latitude).multiply(c)
            var d2 = v.multiply(hhh)
            var d3 = u.multiply(d1)
            d += d3.subtract(d2)

        }
        var g1 = ring[ring.size - 1]
        var point = ring[0]


//        var eee = g1.longitude * c * Math.cos(g1.latitude * hq)
        var eee1 = BigDecimal(g1.longitude).multiply(c)
        var eee2 = Math.cos(BigDecimal(g1.latitude).multiply(hq).toDouble())
        var eee = eee1.multiply(BigDecimal(eee2))


//        var g2 = g1.latitude * c
        var g2 = BigDecimal(g1.latitude).multiply(c)

//        var k = point.longitude * c * Math.cos(point.latitude * hq)
        var k1 = BigDecimal(point.longitude).multiply(c)
        var k2 = Math.cos(BigDecimal(point.latitude).multiply(hq).toDouble())
        var k = k1.multiply(BigDecimal(k2))


//        d += eee * point.latitude * c - k * g2
        var d1 = BigDecimal(point.latitude).multiply(c)
        var d2 = eee.multiply(d1)
        var d3 = k.multiply(g2)
        d += d2.subtract(d3)

//        return BigDecimal(0.5 * Math.abs(d))
        return Math.floor(Math.abs(d.multiply(BigDecimal(0.5)).toDouble()))//向下取整
    }
    class MyLocation(
        val latitude: Double,
        val longitude: Double
    )
}
